package com.taskschecker;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.taskschecker.db.DBManager;
import com.taskschecker.db.TaskInfo;

public class TasksWidget extends AppWidgetProvider {

	private Calendar now = Calendar.getInstance();

	RemoteViews remoteViews;
	AppWidgetManager appWidgetManager;
	ComponentName thisWidget;
	DBManager mgr;
	PendingIntent pendingIntent;
	Intent intent;

	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	public void onReceive(Context context, Intent intent) {

		if (remoteViews == null) {
			remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.tasks_checker_widget);
		}

		if (thisWidget == null) {
			thisWidget = new ComponentName(context, TasksWidget.class);
		}

		if (mgr == null) {
			mgr = new DBManager(context);
		}

		HashMap<String, String> dayMap = getWidgetInfo(now, "day");
		remoteViews.setTextViewText(R.id.widget_day,
				CalendarUtils.getDateYMD(now));
		remoteViews.setTextViewText(R.id.widget_day_percent,
				dayMap.get("dayPercent"));
		remoteViews.setTextViewText(R.id.widget_day_finished_count,
				dayMap.get("dayCount"));

		HashMap<String, String> monthMap = getWidgetInfo(now, "month");
		remoteViews.setTextViewText(R.id.widget_month, context.getResources()
				.getString(R.string.current_month));
		remoteViews.setTextViewText(R.id.widget_month_percent,
				monthMap.get("dayPercent"));
		remoteViews.setTextViewText(R.id.widget_month_finished_count,
				monthMap.get("dayCount"));

		intent = new Intent(context, TasksCheckerActivity.class);
		pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.widget_day, pendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.widget_day_percent,
				pendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.widget_day_finished_count,
				pendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.widget_month, pendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.widget_month_percent,
				pendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.widget_month_finished_count,
				pendingIntent);

		Intent intent2 = new Intent();
		intent2.setAction("cmn.taskschecker.widget.update");
		remoteViews.setOnClickPendingIntent(R.id.widget_refresh,
				PendingIntent.getBroadcast(context, 0, intent2, 0));

		AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,
				remoteViews);

		super.onReceive(context, intent);
	}

	private HashMap<String, String> getWidgetInfo(Calendar now, String flg) {

		HashMap<String, String> map = new HashMap<String, String>();
		List<TaskInfo> tmpTaskList = null;

		if ("day".equals(flg)) {
			int tmpDay = CalendarUtils.getDateInt(now);
			tmpTaskList = getData(tmpDay);
		} else {
			String tmpMonth = CalendarUtils.getDateYM(now).replace("/", "");
			int from = Integer.valueOf(tmpMonth + "01");
			int to = Integer.valueOf(tmpMonth + "31");
			tmpTaskList = getData(from, to);
		}

		double compCnt = 0;
		double cnt = 0;
		for (TaskInfo info : tmpTaskList) {
			cnt++;
			if (info.getCompleteness() == 1) {
				compCnt++;
			}
		}
		if (cnt == 0) {
			map.put("dayPercent", "-");
			map.put("dayCount", "0/0");
		} else {
			double rate = compCnt / cnt;
			DecimalFormat df = new DecimalFormat("##%");
			map.put("dayPercent", df.format(rate));

			int compCntInt = (int) compCnt;
			int cntInt = (int) cnt;
			map.put("dayCount",
					String.valueOf(compCntInt) + "/" + String.valueOf(cntInt));
		}

		return map;
	}

	private List<TaskInfo> getData(Integer day) {
		return mgr.getTaskInfoListByDate(day);
	}

	private List<TaskInfo> getData(Integer from, Integer to) {
		return mgr.getTaskInfoListByDate(from, to);
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
	}

}
